﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SR.API
{
    public static class Utility
    {
        public static SPSite GetSPSite()
        {
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    return site;
            }
            catch (Exception ex)
            {
                throw ex.AppendException("Cannnot open SPSite");
            }
        }

        public static SPWeb GetSPWeb(string siteUrl)
        {
            try
            {
                using (SPWeb web = GetSPSite().OpenWeb(siteUrl))
                    return web;
            }
            catch (Exception ex)
            {
                throw ex.AppendException("Cannot open site");
            }
        }


        public static SPList GetSPList(string listName)
        {
            return GetSPList(listName, "/");
        }

        public static SPList GetSPList(string title, string siteUrl)
        {
            try
            {
                return GetSPWeb(siteUrl).Lists.TryGetList(title);
            }
            catch (Exception ex)
            {
                throw ex.AppendException("Cannot open list.");
            }

        }


        public static string GetStringFieldValue(this SPListItem item, string fieldName)
        {
            if (!fieldName.IsNullOrEmpty())
            {
                if (item.Fields.ContainsField(fieldName))
                {
                    try { return Convert.ToString(item[fieldName]); }
                    catch { return null; }
                }
                else
                    return null;
            }
            else return null;
        }

        public static bool GetBoolFieldValue(this SPListItem item, string fieldName)
        {
            if (fieldName.IsNullOrEmpty())
            {
                if (item.Fields.ContainsField(fieldName))
                {
                    try { return Convert.ToBoolean(item[fieldName]); }
                    catch { return false; }
                }
                else
                    return false;
            }
            else return false;
        }

        #region ================SPQuery====================
        public static SPQuery GetSPQuery()
        {
            SPQuery query = new SPQuery();
            StringBuilder queryText = new StringBuilder(GetOrderByModified());
            query.Query = queryText.ToString();
            return query;
        }

        private static string GetOrderByModified()
        {
            return "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"False\" /></OrderBy>";
        }

        private static string GetContainsStatemen(string type, string name, string value)
        {
            if (type.IsNullOrEmpty() || name.IsNullOrEmpty() || value.IsNullOrEmpty())
                return "<Contains><FieldRef Name=\"" + name + "\" /><Value Type=\"" + type + "\">" + value + "</Value></Contains>";
            else
                return "";
        }
        #endregion=====================SPQuery=============



        #region===============string===============
        public static bool IsNullOrEmpty(this string str)
        {
            return String.IsNullOrEmpty(str);
        }
        #endregion

        public static Exception AppendException(this Exception ex, string str)
        {
            return new Exception("<li>" + ex.Message + " ::" + str + ":: </li>");
        }
    }
}
